package com.lzm.listendemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.lzm.listendemo.R;
import com.lzm.listendemo.bean.Article;

import java.util.List;

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {
    List<Article> items;
    private onItemClick click = null;
    public ArticleAdapter(List<Article> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public ArticleAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_article, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ArticleAdapter.ViewHolder holder, int position) {
        Article item = items.get(position);
        holder.text.setText(item.getTitle());
        holder.tv_author.setText(item.getAuthor());
        holder.tv_niceShareDate.setText(item.getNiceShareDate());
        holder.tv_chapterName.setText(item.getChapterName());
        String source = "";
        if(item.getLink().contains("juejin")){
            holder.iv_source.setImageResource(R.mipmap.ic_juejin);
            source = "juejin";
        }
        if(item.getLink().contains("weixin")){
            holder.iv_source.setImageResource(R.drawable.ic_wechat);
            source = "weixin";
        }
        if(item.getLink().contains("csdn")){
            holder.iv_source.setImageResource(R.drawable.ic_csdn);
            source = "csdn";
        }
        if(item.getLink().contains("wanandroid")){
            holder.iv_source.setImageResource(R.drawable.ic_android);
            source = "wanandroid";
        }
        if(click!=null){
            holder.itemView.setOnClickListener(v->click.onClick(v,item));
        }
        item.setSource(source);
        ViewCompat.setTransitionName(holder.text,"title:"+item.getTitle());
        ViewCompat.setTransitionName(holder.iv_source,"source:"+source);
    }

    public interface onItemClick{
        void onClick(View view,Article article);
    }


    @Override
    public int getItemCount() {
        return items.size();
    }

    public void setList(List<Article> articles) {
        this.items = articles;
        notifyDataSetChanged();
    }

    public void setItemOnClick(onItemClick onClick) {
        this.click = onClick;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView text;
        TextView tv_author;
        TextView tv_chapterName;
        TextView tv_niceShareDate;
        ImageView iv_source;

        public ViewHolder(View view) {
            super(view);
            text = (TextView) view.findViewById(R.id.text);
            tv_author = (TextView) view.findViewById(R.id.tv_author);
            tv_chapterName = (TextView) view.findViewById(R.id.tv_chapterName);
            tv_niceShareDate = (TextView) view.findViewById(R.id.tv_niceShareDate);
            iv_source = (ImageView) view.findViewById(R.id.iv_source);
        }

    }

}